package com.controller.client;

import com.dao.Flight;
import com.dao.User;
import com.rmi.RMIBrokerInterface;
import com.rmi.RMITLSClientSocketFactory;
import com.util.Constant;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.List;

/**
 * Created by Phillip on 2014/4/19.
 */
public class RMIClient {

    private Registry registry;
    private RMIBrokerInterface stub;
    private Logger logger;
    public RMIClient() {

        try {

            logger = Logger.getLogger(RMIClient.class);

            PropertyConfigurator.configure(Constant.CLIENT_PRO_STRING);
            registry = LocateRegistry.getRegistry("localhost", 1099,
                    new RMITLSClientSocketFactory(Constant.SER_KEYSTORE_STRING, Constant.CLI_KEYSTORE_STRING, "huyifei"));
            stub = (RMIBrokerInterface) registry.lookup(Constant.RMIBROKER_STRING);

            logger.debug("get the stub from BrokerRMIServer");

        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (NotBoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

    }

    public List<Flight> queryFlights(int table, String deptString, String destString, String deptDateString, String returnDateString) throws RemoteException {
        logger.debug("request queryFlights on server " + table);
        return stub.queryFlights(table, deptString, destString, deptDateString, returnDateString);
    }

    public List<String> queryCity(int table) throws RemoteException {
        logger.debug("request queryCity on server " + table);
        return stub.queryCity(table);
    }

    public String book(int table, String flightNoString1, String deptString, String clasString1, String flightNoString2, String returnString, String classString2, String nameString, String creditString) throws RemoteException {
        logger.debug("request book on server " + table);
        return stub.book(table, flightNoString1, deptString, clasString1, flightNoString2, returnString, classString2, nameString, creditString);
    }

    public String queryRates(int table, String flightString, String classString) throws RemoteException {
        logger.debug("request queryRates on server " + table);
        return stub.queryRates(table, flightString, classString);
    }

    public String queryAirline(int table, String deptString, String dsctString) throws RemoteException {
        logger.debug("request queryAirline on server " + table);
        return stub.queryAirline(table, deptString, dsctString);
    }

    public void bye() {
        logger.info("RMIClient is shutting down the connection...");

    }

    public boolean createUser(User user) throws RemoteException{
        logger.debug("request createUser");
        return stub.createUser(user);
    }

    public User getUser(String username) throws RemoteException{
        logger.debug("request a user by the name: " + username);
        return stub.getUser(username);
    }
}
